﻿using System;
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PMS.BLL;
using PMS.Entity;
using PMS;
using PMS.Resources;
using PMS.Win;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Base;

namespace PMS.ChucNang
{
    public partial class DongMoKyForm : FormManualBase
    {
        private const string XacNhanDongKYMessage = "XacNhanDongKYMessage";
        private const string DongMoKyErrorMessage = "DongMoKyErrorMessage";
        private readonly DongMoKyBO service = new DongMoKyBO();
        public DongMoKy DongMoKySelected;
        public DongMoKyForm(string functionID)
            : base(functionID)
        {
            InitializeComponent();
            InitStyle();
            InitData();
        }

        void InitStyle()
        {
            grvDongMoKyList.OptionsBehavior.Editable = true;
            grvDongMoKyList.OptionsView.ShowGroupPanel = false;
            grvDongMoKyList.OptionsView.ShowIndicator = true;
            //grvDongMoKyList.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.MouseDownFocused;
            grvDongMoKyList.OptionsView.ShowAutoFilterRow = true;
            grvDongMoKyList.OptionsView.ShowFilterPanelMode = ShowFilterPanelMode.Never;
            grvDongMoKyList.OptionsView.ShowGroupedColumns = false;
            grvDongMoKyList.OptionsMenu.EnableColumnMenu = false;
            grvDongMoKyList.OptionsCustomization.AllowColumnMoving = false;
            grvDongMoKyList.OptionsCustomization.AllowGroup = false;
            grvDongMoKyList.OptionsFilter.AllowFilterEditor = false;

            this.grvDongMoKyList.OptionsSelection.MultiSelect = false;
            this.grvDongMoKyList.IndicatorWidth = Constants.INDICATOR_WIDTH;
            //gridbinding
            this.colMaKy.FieldName = DongMoKyColumns.MaKy.ToString();
            this.colNgayMo.FieldName = DongMoKyColumns.NgayMoKy.ToString();
            this.colNgayDong.FieldName = DongMoKyColumns.NgayDongKy.ToString();
            this.colNguoiDong.FieldName = DongMoKyColumns.NguoiCapNhap.ToString();
            this.colGhiChu.FieldName = DongMoKyColumns.GhiChu.ToString();
            this.colTinhTrang.UnboundType = DevExpress.Data.UnboundColumnType.String;            

        }

        void InitData()
        {
            try
            {
                
                this.grdDongMoKyList.DataSource = service.GetAllDongMoKy();
                    
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }

        }


        private void grvDongMoKyList_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if ((e.Info.IsRowIndicator) && (e.RowHandle >= 0))
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void grvDongMoKyList_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            
            DongMoKySelected = this.grvDongMoKyList.GetFocusedRow() as DongMoKy;
        }

        private void grvDongMoKyList_ShowingEditor(object sender, CancelEventArgs e)
        {
            //e.Cancel = true; //ko cho phep edit du lieu trong gridview
            e.Cancel = false;  //cho phep edit du lieu trong gridview

        }

        private void btnDongKy_Click(object sender, System.EventArgs e)
        {
            try
            {
                if (DongMoKySelected != null&&DongMoKySelected.DongKy!=true)
                {
                    XacNhanDongMoKy form = new XacNhanDongMoKy(Messages.GetString(XacNhanDongKYMessage));
                    if (form.ShowDialog() == DialogResult.OK)
                    {
                        DongMoKySelected.NguoiCapNhap = CurrentUserName;
                        DongMoKySelected.DongKy = true;
                        service.DongKy(DongMoKySelected);
                        
                    }
                    InitData();
                }
                else
                {
                    ShowErrorMessage(Messages.GetString(DongMoKyErrorMessage));
                }
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
                InitData();
            }
        }

        private void grvDongMoKyList_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
        {
            DongMoKy tempRow = (DongMoKy)this.grvDongMoKyList.GetRow(e.RowHandle);
            if ((e.Column == colTinhTrang) && e.IsGetData && e.RowHandle >= 0)
            {
                if (tempRow.DongKy)
                {
                    e.Value = "Đóng";

                }
                else
                {
                    e.Value = "Mở";
                }

            }
        }
    }
}
